package cn.wolfcode.luowowo.wesite.until;

import cn.wolfcode.luowowo.cache.until.RedisKeys;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by LENOVO on 2019/8/8.
 */
public class CookieUtil {
    public static void addCookie(HttpServletResponse response, String name, String value,int timeOut){
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");//设置为根路径,表示该任何cookie的请求都可以共享
        cookie.setMaxAge(timeOut);//设置cookie存活的事件
        response.addCookie(cookie);
    }

    public static String getToken(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        //判断cookie是否为空
        if(cookies !=null){
            for (Cookie cookie:cookies){
                if (cookie.getName().equals("token")){
                    return cookie.getValue();
                }
            }
        }

        return null;
    }
}
